package org.appframe4gwt.model;

import xq.gwt.mvc.model.BooleanPropertyModel;
import xq.gwt.mvc.model.CommandModel;
import xq.gwt.mvc.model.IntegerPropertyModel;
import xq.gwt.mvc.model.PasswordPropertyModel;
import xq.gwt.mvc.model.PersistableEntityModel;
import xq.gwt.mvc.model.PropertyChangedEvent;
import xq.gwt.mvc.model.PropertyChangedListener;
import xq.gwt.mvc.model.PropertyModel;
import xq.gwt.mvc.model.PropertyModelList;
import xq.gwt.mvc.model.StringPropertyModel;
import xq.gwt.ui.crud.localization.CrudDictionary;


public class User extends PersistableEntityModel implements PropertyChangedListener{
	
	private static final long serialVersionUID = 163121832442124381L;

	protected IntegerPropertyModel id;
	protected StringPropertyModel name;
	protected StringPropertyModel displayName;	
	protected StringPropertyModel role;
	protected PasswordPropertyModel password;
	protected BooleanPropertyModel blocked;
	protected PropertyModel[] props;
	protected PropertyModel[] keyProps;	
	protected PropertyModelList propertyList = new PropertyModelList();

	public User() {
		init();
	}

	private void init() {
		id = new IntegerPropertyModel();
		id.setName("ID");
		id.setTitle("ИД");
		id.setHidden(true);

		blocked = new BooleanPropertyModel();
		blocked.setName("BLOCKED");
		blocked.setTitle(CrudDictionary.Constants().blocked_caption());

		name = new StringPropertyModel();
		name.setName("NAME");
		name.setTitle(CrudDictionary.Constants().name_caption());
		name.setRequired(true);
		
		displayName = new StringPropertyModel();
		displayName.setName("DISPLAY_NAME");
		displayName.setTitle(CrudDictionary.Constants().display_name_caption());
				
		role = new StringPropertyModel();
		role.setName("ROLE");
		role.setTitle(CrudDictionary.Constants().role_caption());		
		role.setLimitToList(true);
		
		password = new PasswordPropertyModel();
		password.setName("PASSWORD");
		password.setTitle(CrudDictionary.Constants().password_caption());

		props = new PropertyModel[] { id, name, displayName,role, password, blocked };
		propertyList.add(props);
		propertyList.addPropertyChangedListener(this);
		keyProps = new PropertyModel[] { id };
	}

	@Override
	public String getName() {
		return "USER";
	}

	
	@Override
	public String getTitle() {
		return "Users";
	}

	
	@Override
	public PropertyModel[] getProperties() {
		return props;
	}

	
	@Override
	public CommandModel[] getCommands() {		
		return null;
	}

	
	@Override
	public PropertyModel[] getKeyProperties() {
		return keyProps;
	}

	@Override
	public PersistableEntityModel newInstance() {
		return new User();
	}

	@Override
	public void propertyChanged(PropertyChangedEvent event) {
		notifyEnityChanged();		
	}

	public StringPropertyModel getDislayName() {
		return displayName;
	}

	public StringPropertyModel getRoleName() {
		return role;
	}

	public StringPropertyModel getUserName(){
		return name;
	}
	
	public StringPropertyModel getPassword(){
		return password;
	}

	public IntegerPropertyModel getId() {
		return id;
	}	
}

